<?php

    function getPages($cur_page,$per_page,$show_page,$table,$link)
    {

        //$cur_page = $_GET['p'];
        //$per_page = 1;
        //$offset = ($cur_page - 1)*$per_page;

        //$show_page = 5;
        $c =$GLOBALS['c'];
        $a =$GLOBALS['a'];
        $offset_page = floor($show_page/2);

        $sql = 'select count(*) as total from `'.$table.'`';

        $total_result = mysqli_query($link,$sql);

        //print_r($total_result);die;

        $total_num = mysqli_fetch_assoc($total_result)['total'];
        //echo $total_num;die;
        $total_page = ceil($total_num/$per_page);
        //echo $total_page;
        $start = $cur_page - $offset_page;
        $end = $cur_page + $offset_page;

        if ($total_page < $show_page) {
            $show_page = $total_page;
        }

        if($start < 1){
            $start = 1;
            $end = $show_page;
        };

        if($end > $total_page){
            $end = $total_page;
            $start = $total_page - $show_page + 1;
        }

        $href = '<li><a href="index.php?c='.$c.'&a='.$a.'&admin=1&p=';

        //$first = '<li><a href="index.php?c='.$c.'&a='.$a.'&admin=1&p=';


        $pages = '';

        if($cur_page != 1){
            $pages .=$href.'1">首页</a></li>'.$href.($cur_page-1).'">上一页</a></li>';

        }else{
            $pages .='<li><a href="#">没有了</a></li>';
        };

        for($i = $start;$i <= $end;$i++){
                $pages .=$href. $i.'">'.$i.'</a></li>';
        }

        if($cur_page != $total_page){
            $pages .=$href.($cur_page+1).'">下一页</a></li>'.$href.$total_page.'">尾页</a></li>';
        }else{
            $pages .='<li><a href="#">没有了</a></li>';
        };

        return $pages;
    };
?>